const connection = require('../app/database')

class LabelService {
    async create(content) {
        const statement = `INSERT INTO label (name) VALUES (?);`;
        const [result] = await connection.execute(statement, [content])
        return result
    }
    async getList(offset, limit) {
        const statement = `SELECT * FROM label LIMIT ?, ?;`;
        const [result] = await connection.execute(statement, [offset, limit])
        return result
    }
    async getListByMomentId(momentId) {
        const statement = `
        SELECT la.id id,la.name name FROM 
        label la
        LEFT JOIN moment_label ml ON ml.label_id = la.id
        WHERE ml.moment_id = ?;
        `;
        const [result] = await connection.execute(statement, [momentId])
        return result
    }
    async getLabelByName(name) {
        const statement = `
        SELECT la.id id,la.name name FROM 
        label la
        WHERE la.name = ?;
        `;
        const [result] = await connection.execute(statement, [name])
        return result
    }
    async hasLabel(momentId, labelId) {
        const statement = `SELECT * FROM moment_label WHERE moment_id = ? AND label_id = ?;`
        const [result] = await connection.execute(statement, [momentId, labelId])
        return result[0] ? true : false
    }
}

module.exports = new LabelService()